import matplotlib.pyplot as plt
import pandas as pd;
import matplotlib.gridspec as gridspec
import numpy as np;
from openpyxl.workbook import Workbook
plt.rcParams['font.sans-serif']='STSong'

womendata=pd.read_excel('./体测分数_女生.xls')
mandata=pd.read_excel('./体测分数_男生.xls')

gs=gridspec.GridSpec(7,2);
man1=pd.cut(mandata['男1000米跑'],bins=3,
            labels=['一般','良好','优秀'])

# 男引体
man2=pd.cut(mandata['男引体'],bins=3,
            labels=['一般','良好','优秀'])

print(man2.value_counts().index)
plt.figure(figsize=(12,8))
print(man2.value_counts())
plt.subplot(gs[:2,:1])
plt.pie(man2.value_counts(),
        radius=1.2,
        labels=man2.value_counts().index,
        autopct='%0.2f%%',
        pctdistance=0.75,
        wedgeprops=dict(linewidth=3,width = 0.6)
        )

plt.title('男1000米跑成绩占比',pad=20)
plt.subplot(gs[:2,1:2])
plt.pie(man1.value_counts(),
        radius=1.2,
        labels=man1.value_counts().index,
        autopct='%0.2f%%',
        pctdistance=0.75,
        wedgeprops=dict(linewidth=3,width = 0.6)
        )
plt.title('男引体成绩占比',pad=20)
women3=pd.qcut(womendata['女800米跑'],q = 4,# 4等分
               labels=['差','中','良','优'])
women4=pd.qcut(womendata['女跳远'],q = 4,# 4等分
               labels=['差','中','良','优'])
plt.subplot(gs[2:4,:1])
colors = ['#ff0000', '#c5b783', '#3c7f99', '#0000cd']

#count,bins,flg=plt.hist(x,bins=100,color='red');
# plt.bar(x=women3.value_counts().index,
#         height=women3.value_counts(),
#         color=colors)

count,bins,flg=plt.hist(women3,bins=10,color='#FF3388');
ax = plt.gca()
ax.spines['right'].set_color('white')
ax.spines['top'].set_color('#FFFFFF')
plt.title('女800米跑')
plt.subplot(gs[2:4,1:2])

count,bins,flg=plt.hist(women4,bins=10,color='red');
ax = plt.gca() # 获取当前视图
# 右边和上⾯脊柱消失
ax.spines['right'].set_color('white')
ax.spines['top'].set_color('#FFFFFF')
plt.title('女跳远')
# plt.show()
#使用嵌套饼图对比男女生体重指数进行比例统计，分为正常、低体重、超重、肥胖(男女生体重指数参考如下)

bim=np.round(womendata['BMI'],2);
def get_level(x):
    y=np.round(x['BMI'],2);
    if y>=25.3:
        x['type1']='肥胖'
    elif y<25.3 and y>=22.8:
        x['type1']='超重'
    elif y>16.5 and y<=22.7:
        x['type1']='正常'
    else:
        x['type1']='低体重'
    return x;
womendata=womendata.apply(get_level,axis=1)

wm=womendata.groupby('type1')['BMI'].apply(np.count_nonzero).round(1)
def get_levelmen(x):
    y=np.round(x['BMI'],2);
    if y>=26.4:
        x['type1']='肥胖'
    elif y<26.4 and y>=23.3:
        x['type1']='超重'
    elif y>16.5 and y<=23.2:
        x['type1']='正常'
    else:
        x['type1']='低体重'
    return x;
mandata=mandata.apply(get_levelmen,axis=1)
#city_salary = job.groupby("city")["salary"].
man=mandata.groupby('type1')['BMI']. \
    apply(np.count_nonzero).round(2)
# print(wm.index,wm.values)
plt.subplot(gs[5:7,:1])

plt.pie(wm.values,
        labels=wm.index,
        autopct='%0.2f%%',
        radius=1.5, # 半径
        pctdistance=0.9, # 百分⽐位置
        wedgeprops=dict(linewidth=3,width=0.6,edgecolor='w'))

plt.legend(wm.index,loc = 'upper right'
           ,bbox_to_anchor = (1,0,1.2,1),title =
           '女BIM指标占⽐',)
plt.pie(man.values,
        autopct='%0.2f%%',
        radius=0.9, # 半径
        pctdistance=0.9, # 百分⽐位置
        wedgeprops=dict(linewidth=3,width=0.9,edgecolor='w'))

plt.subplots_adjust(left=None, bottom=None,
                    right=None, top=None,wspace=0.5,
                    hspace=0.5)
plt.title('男女生体重指数',pad=20)
plt.show()

# print(mandata['男1000米跑'])

